<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn"%>

<c:if test="${buyerAddressList!=null&&fn:length(buyerAddressList)>0 }">
	<div class="modal-header">
		<h4 class="modal-title text-center">选择收货地址</h4>
	</div>
	<div class="modal-body">
		<c:forEach items="${buyerAddressList }" var="ba" varStatus="baStat">
			<div class="row margin-top-10">
				<div class="col-xs-2 padding-top-10">
					<c:if test="${ba.buyerAddressDefault=='1' }">
						<input type="radio" checked="checked" name="address_radio" class="address_radio_input" id="address_radio_${ba.buyerAddressId }" data-id="${ba.buyerAddressId }" data-name="${ba.buyerAddressName }"
							data-phone="${ba.buyerAddressPhone }" data-detail="${ba.buyerAddressDetail }">
					</c:if>
					<c:if test="${ba.buyerAddressDefault=='0' }">
						<input type="radio" name="address_radio" class="address_radio_input" id="address_radio_${ba.buyerAddressId }" data-id="${ba.buyerAddressId }" data-name="${ba.buyerAddressName }"
							data-phone="${ba.buyerAddressPhone }" data-detail="${ba.buyerAddressDetail }">
					</c:if>
				</div>
				<div class="col-xs-10">
					<div class="address_detail_button" data-target="#address_radio_${ba.buyerAddressId }">
						<span>${ba.buyerAddressDetail }</span> <br> <span>${ba.buyerAddressName }</span> <span>${ba.buyerAddressPhone }</span>
					</div>
				</div>
			</div>
		</c:forEach>
	</div>
	<div class="modal-footer">
		<button type="button" class="btn default pull-left border-radius-5" data-dismiss="modal">关闭</button>
		<a type="button" class="btn blue pull-left border-radius-5" href="show_buyer_address_list">新增</a>
		<button type="button" class="btn blue pull-right border-radius-5" id="select_address_confirm_button">选择</button>
	</div>
</c:if>
<c:if test="${buyerAddressList==null||fn:length(buyerAddressList)==0 }">
	<div class="modal-header">
		<h4 class="modal-title text-center">添加收货地址</h4>
	</div>
	<div class="modal-body" style="padding-bottom: 0px !important;">
		<div class="row">
			<div class="col-xs-12">
				<div class="form-group">
					<label for="peopleName">*收货人姓名(2-20字):</label>
					<input type="text" class="form-control" id="peopleName" placeholder="收货人姓名" data-regex="^\S.{0,18}\S$">
				</div>
			</div>
		</div>
		<div class="row">
			<div class="col-xs-12">
				<div class="form-group">
					<label for="peopleCellPhone">*收货人手机号:</label>
					<input type="tel" class="form-control" id="peopleCellPhone" placeholder="收货人手机号" data-regex="^\S.{0,48}\S$">
				</div>
			</div>
		</div>
		<div class="row">
			<div class="col-xs-12">
				<div class="form-group">
					<label for="peopleAddress">*所在地区:</label>
					<div class="row">
						<div class="col-xs-4">
							<select id="seachprov" class="form-control" name="seachprov" onChange="changeComplexProvince(this.value, sub_array, 'seachcity', 'seachdistrict');"></select>
						</div>
						<div class="col-xs-4">
							<select id="seachcity" name="homecity" class="form-control" onChange="changeCity(this.value,'seachdistrict','seachdistrict');"></select>
						</div>
						<div class="col-xs-4">
							<span id="seachdistrict_div"><select id="seachdistrict" class="form-control" name="seachdistrict"></select></span>
						</div>
					</div>
				</div>
			</div>
		</div>
		<div class="row">
			<div class="col-xs-12">
				<div class="form-group">
					<label for="peopleAddress">*详细地址(4-50字):</label>
					<input type="text" class="form-control" id="peopleAddress" placeholder="详细地址" data-regex="^\S.{2,48}\S$">
				</div>
			</div>
		</div>
		<div class="row">
			<div class="col-xs-12">
				<input type="checkbox" name="" id="peopleAddressDefault" class="">
				<label for="peopleAddressDefault">设为默认地址</label>
			</div>
		</div>
	</div>
	<div class="modal-footer">
		<button type="button" class="btn default pull-left border-radius-5" data-dismiss="modal">关闭</button>
		<button type="button" class="btn blue pull-right border-radius-5" id="add_address_submit_button">提交</button>
	</div>

	<script type="text/javascript">
		function initComplexArea(a, k, h, p, q, d, b, l)
		{
			var f = initComplexArea.arguments;
			var m = document.getElementById(a);
			var o = document.getElementById(k);
			var n = document.getElementById(h);
			var e = 0;
			var c = 0;
			if (p != undefined)
			{
				if (d != undefined)
				{
					d = parseInt(d);
				}
				else
				{
					d = 0;
				}
				if (b != undefined)
				{
					b = parseInt(b);
				}
				else
				{
					b = 0;
				}
				if (l != undefined)
				{
					l = parseInt(l);
				}
				else
				{
					l = 0
				}
				n[0] = new Option("请选择 ", 0);
				for (e = 0; e < p.length; e++)
				{
					if (p[e] == undefined)
					{
						continue;
					}
					if (f[6])
					{
						if (f[6] == true)
						{
							if (e == 0)
							{
								continue
							}
						}
					}
					m[c] = new Option(p[e], e);
					if (d == e)
					{
						m[c].selected = true;
					}
					c++
				}
				if (q[d] != undefined)
				{
					c = 0;
					for (e = 0; e < q[d].length; e++)
					{
						if (q[d][e] == undefined)
						{
							continue
						}
						if (f[6])
						{
							if ((f[6] == true) && (d != 71) && (d != 81) && (d != 82))
							{
								if ((e % 100) == 0)
								{
									continue
								}
							}
						}
						o[c] = new Option(q[d][e], e);
						if (b == e)
						{
							o[c].selected = true
						}
						c++
					}
				}
			}
		}
		function changeComplexProvince(f, k, e, d)
		{
			var c = changeComplexProvince.arguments;
			var h = document.getElementById(e);
			var g = document.getElementById(d);
			var b = 0;
			var a = 0;
			removeOptions(h);
			f = parseInt(f);
			if (k[f] != undefined)
			{
				for (b = 0; b < k[f].length; b++)
				{
					if (k[f][b] == undefined)
					{
						continue
					}
					if (c[3])
					{
						if ((c[3] == true) && (f != 71) && (f != 81) && (f != 82))
						{
							if ((b % 100) == 0)
							{
								continue
							}
						}
					}
					h[a] = new Option(k[f][b], b);
					a++
				}
			}
			removeOptions(g);
			g[0] = new Option("请选择 ", 0);
			if (f == 71 || f == 81 || f == 82)
			{
				if ($("#" + d + "_div"))
				{
					$("#" + d + "_div").hide();
				}
			}
			else
			{
				if ($("#" + d + "_div"))
				{
					$("#" + d + "_div").show();
				}
			}
		}

		function changeCity(c, a, t)
		{
			$("#" + a).html('<option value="0" >请选择</option>');
			$("#" + a).unbind("change");
			c = parseInt(c);
			var _d = sub_arr[c];
			var str = "";
			str += "<option value='0' >请选择</option>";
			for (var i = c * 100; i < _d.length; i++)
			{
				if (_d[i] == undefined)
					continue;
				str += "<option value='" + i + "' >" + _d[i] + "</option>";
			}
			$("#" + a).html(str);

		}

		function removeOptions(c)
		{
			if ((c != undefined) && (c.options != undefined))
			{
				var a = c.options.length;
				for (var b = 0; b < a; b++)
				{
					c.options[0] = null;
				}
			}
		}

		//得到地区码
		function getAreaID()
		{
			var area = 0;
			if ($("#seachdistrict").val() != "0")
			{
				area = $("#seachdistrict").val();
			}
			else if ($("#seachcity").val() != "0")
			{
				area = $("#seachcity").val();
			}
			else
			{
				area = $("#seachprov").val();
			}
			return area;
		}

		function showAreaID()
		{
			//地区码
			var areaID = getAreaID() == null ? "" : getAreaID();
			//地区名
			var areaName = getAreaNamebyID(areaID);
			alert("您选择的地区码：" + areaID + "      地区名：" + areaName);
		}

		//根据地区码查询地区名
		function getAreaNamebyID(areaID)
		{
			var areaName = "";
			if (areaID.length == 2)
			{
				areaName = area_array[areaID];
			}
			else if (areaID.length == 4)
			{
				var index1 = areaID.substring(0, 2);
				areaName = area_array[index1] + "," + sub_array[index1][areaID];
			}
			else if (areaID.length == 6)
			{
				var index1 = areaID.substring(0, 2);
				var index2 = areaID.substring(0, 4);
				areaName = area_array[index1] + "," + sub_array[index1][index2] + "," + sub_arr[index2][areaID];
			}
			return areaName;
		}

		initComplexArea('seachprov', 'seachcity', 'seachdistrict', area_array, sub_array, '44', '0', '0');
	</script>
</c:if>
<script type="text/javascript">
	$(function()
	{
		$("input:radio,input:checkbox").uniform();
		$.uniform.update();
		$("input:radio,input:checkbox").click(function()
		{
			$.uniform.update();
		});
		$(".address_detail_button").each(function()
		{
			var obj = $(this);
			obj.click(function()
			{
				$(obj.attr("data-target")).trigger("click");
				$.uniform.update();
			});
		});
		//选择收货地址
		$("#select_address_confirm_button").click(function()
		{
			if ($('input[name="address_radio"]:checked').length <= 0)
			{
				alert("请先选择收货地址");
				return false;
			}
			var detail = $(".address_radio_input:checked").attr("data-detail");
			var name = $(".address_radio_input:checked").attr("data-name");
			var phone = $(".address_radio_input:checked").attr("data-phone");
			var html = "<span>" + detail + "</span><br><span>" + name + "</span><span>" + phone + "</span>";
			$("#show_address_detail_div").html(html);
			$("#address_list_modal").modal('hide');
			$("#addressId_input").val($(".address_radio_input:checked").attr("data-id"));
		});

		//添加收货地址
		$("#add_address_submit_button").click(function()
		{
			var peopleName = $("#peopleName").val();
			var peopleCellPhone = $("#peopleCellPhone").val();
			var peopleAddress = $("#peopleAddress").val();
			var addressDefault;

			//地区码
			var areaID = getAreaID() == null ? "" : getAreaID();
			//地区名
			var areaName = getAreaNamebyID(areaID);

			var areaNameArray = new Array(); //定义一数组 
			areaNameArray = areaName.split(","); //字符分割
			if (areaNameArray.length <= 1)
			{
				alert("所在地区为必选项");
				return false;
			}

			if ($("#peopleAddressDefault").is(':checked'))
			{
				addressDefault = true;
			}
			else
			{
				addressDefault = false;
			}

			if (peopleName.length <= 0 || peopleCellPhone.length <= 0 || peopleAddress.length <= 0)
			{
				alert("标有*的输入项都是必填项.")
				return false;
			}

			var tips = "";
			var tips_number = 1;
			$.each([ $("#peopleName"), $("#peopleCellPhone"), $("#peopleAddress") ], function(i, n)
			{
				if (new RegExp(n.attr("data-regex")).test(n.val()) == false)
				{
					tips += tips_number++ + " . \"" + n.val() + "\" 不满足输入要求;\n";
				}
			});
			if (tips.length > 0)
			{
				alert(tips + "\n请检查一下重新输入.");
				return false;
			}

			var add_address_submit_button_html = $("#add_address_submit_button").html();
			$.ajax(
			{
				url : "${basePath}u_add_new_address_do",
				type : "POST",
				dataType : "json",
				beforeSend : function(XMLHttpRequest)
				{
					$("#add_address_submit_button").attr("disabled", "disabled");
					$("#add_address_submit_button").html("正在提交收货地址中...");
				},
				data :
				{
					'buyerAddress.buyerAddressName' : peopleName,
					'buyerAddress.buyerAddressDetail' : peopleAddress,
					'buyerAddress.buyerAddressPhone' : peopleCellPhone,
					'buyerAddress.buyerAddressProvince' : areaNameArray[0],
					'buyerAddress.buyerAddressCity' : areaNameArray[1],
					'buyerAddress.buyerAddressDistrict' : areaNameArray[2],
					"addressDefault" : addressDefault
				},
				success : function(json, textStatus)
				{
					if (json.success == true)
					{
						var detail = json.address.buyerAddressDetail;
						var name = json.address.buyerAddressName;
						var phone = json.address.buyerAddressPhone;
						var html = "<span>" + detail + "</span><br><span>" + name + "</span><span>" + phone + "</span>";
						$("#show_address_detail_div").html(html);
						$("#address_list_modal").modal('hide');
						$("#addressId_input").val(json.address.buyerAddressId);
					}
					else
					{
						alert(json.info);
					}
				},
				error : function(XMLHttpRequest, textStatus, errorThrown)
				{
					alert("提交失败，请仔细检查您的输入是否满足要求。");
				},
				complete : function(XMLHttpRequest, textStatus)
				{
					$("#add_address_submit_button").removeAttr("disabled");
					$("#add_address_submit_button").html(add_address_submit_button_html);
				}
			});
		});

	});
</script>